# 获取当前使用的网卡
function Get-ActiveNetAdapter
{
    # 获取所有网卡
    $adapters = Get-NetAdapter

    # 找到当前使用的网卡
    $activeAdapter = $adapters | Where-Object { $_.Status -eq "Up" }

    # 返回当前使用的网卡
    return $activeAdapter
}
$activeAdapter = Get-ActiveNetAdapter

if ($activeAdapter)
{
    Write-Output "当前使用的网卡："
    Write-Output $activeAdapter.Name
}
else
{
    Write-Output "未找到当前使用的网卡"
}


# 列举所有网卡信息
function Get-NetAdapterInfo {
    # 获取所有网卡
    $adapters = Get-NetAdapter

    # 遍历每个网卡
    foreach ($adapter in $adapters) {
        Write-Output "名称：" $adapter.Name
        Write-Output "状态：" $adapter.Status
        Write-Output "类型：" $adapter.MediaType
        Write-Output "MAC 地址：" $adapter.MACAddress
        Write-Output "IP 地址：" (Get-NetIPConfiguration -InterfaceName $adapter.Name).IPv4Address[0].Address
        Write-Output "-------------------------"
    }
}
Get-NetAdapterInfo



# 根据网卡名称禁用网卡
function Disable-NetAdapterByName {
    # 参数：网卡名称
    param(
        [Parameter(Mandatory=$true)]
        [string]
        $Name
    )

    # 获取指定名称的网卡
    $adapter = Get-NetAdapter -Name $Name

    # 如果找到网卡，则禁用它
    if ($adapter) {
        Disable-NetAdapter -Name $Name -Confirm:$false
        Write-Output "网卡 $Name 已禁用"
    } else {
        Write-Output "未找到名称为 $Name 的网卡"
    }
}
Disable-NetAdapterByName -Name "net2"


# 根据网卡名称启用网卡
function Enable-NetAdapterByName {
    # 参数：网卡名称
    param(
        [Parameter(Mandatory=$true)]
        [string]
        $Name
    )

    # 获取指定名称的网卡
    $adapter = Get-NetAdapter -Name $Name

    # 如果找到网卡，则启用它
    if ($adapter) {
        Enable-NetAdapter -Name $Name -Confirm:$false
        Write-Output "网卡 $Name 已启用"
    } else {
        Write-Output "未找到名称为 $Name 的网卡"
    }
}
Enable-NetAdapterByName -Name "net2"



function Get-ActiveNetAdapterInfo {
    $adapters = Get-NetAdapter
    $activeAdapters = $adapters | Where-Object { $_.Status -eq "Up" }
    foreach ($adapter in $activeAdapters) {
        Write-Output "当前启用的网卡:"
        Write-Output "名称:" $adapter.Name
        Write-Output "状态:" $adapter.Status
        Write-Output "-------------------------"
    }
}
Get-ActiveNetAdapterInfo